package com.ruoyi.controller.achievements;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.service.achievements.AcvDepManageService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 绩效部门管理
 * @date 2020/8/21 16:47
 */
@Controller
@RequestMapping("/acvDepManageController")
public class AcvDepManageController extends BaseController {

    @Autowired
    AcvDepManageService acvDepManageService;

    @Autowired
    private ISysDeptService deptService;

    private String prefix = "achievements/depmanage";

    @GetMapping()
    public String acvDepManage(ModelMap mmp) {
        mmp.put("secondaryDeptList", acvDepManageService.findSecondaryDept());
        return prefix + "/acvDepManage";
    }

    @PostMapping("/list")
    @ResponseBody
    public List<Map> list(@RequestParam Map map) {
        List<Map> deptList = acvDepManageService.finDepList(map);
        return deptList;
    }

    @GetMapping("/add")
    public String add(String deptId, ModelMap mmap) {
        mmap.put("deptId", deptId);
        return prefix + "/add";
    }

    @RequestMapping("/addDep")
    @ResponseBody
    public AjaxResult addDep(String depName, String deptId) {
        return toAjax(acvDepManageService.addDep(depName, deptId));
    }

    @GetMapping("/edit")
    public String edit(String deptId, ModelMap mmap) {
        mmap.put("twoDeptIdList", acvDepManageService.findTwoDeptList());
        mmap.put("dept", acvDepManageService.findDepNameByDeptId(deptId));
        return prefix + "/edit";
    }

    @RequestMapping("/editDep")
    @ResponseBody
    public AjaxResult editDep(String deptId, String depName,String twoDeptId) {
        return toAjax(acvDepManageService.editDepNameByDeptId(deptId, depName,twoDeptId));
    }

    @RequestMapping("/remove/{deptId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deptId") Long deptId) {
        if (deptService.selectDeptCount(deptId) > 0) {
            return AjaxResult.warn("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId)) {
            return AjaxResult.warn("部门存在用户,不允许删除");
        }
        return toAjax(deptService.deleteDeptById(deptId));
    }

    @GetMapping("/addJob")
    public String addJob(String deptId, ModelMap mmap) {
        mmap.put("dept", acvDepManageService.findDepNameByDeptId(deptId));
        return prefix + "/addJob";
    }

    @RequestMapping("/addJob")
    @ResponseBody
    public AjaxResult addJob(@RequestParam Map map) {
        return toAjax(acvDepManageService.addJob(map));
    }

    @GetMapping("/editJob")
    public String editJob(String deptId, ModelMap mmap) {
        mmap.put("job", acvDepManageService.findJobById(deptId));
        return prefix + "/editJob";
    }

    @RequestMapping("/editJob")
    @ResponseBody
    public AjaxResult editJob(@RequestParam Map map) {
        return toAjax(acvDepManageService.editJob(map));
    }

    @RequestMapping("/removeJob")
    @ResponseBody
    public AjaxResult removeJob(@RequestParam Map map) {
        String jobId = map.get("jobId").toString();
        return toAjax(acvDepManageService.deleteJobById(jobId));
    }

    /**
     * 跳转添加二级部门页面
     *
     * @param mmap
     * @return
     */
    @GetMapping("/toAddTwoDept")
    public String toAddTwoDept(ModelMap mmap) {
        return prefix + "/addTwoDept";
    }

    @RequestMapping("/addTwoDept")
    @ResponseBody
    public AjaxResult addTwoDept(@RequestParam Map map) {
        String deptName = map.get("deptName").toString();
        if (acvDepManageService.getTwoDeptCountByDeptName(deptName) > 0) {
            return error(deptName + "已存在!");
        }
        return toAjax(acvDepManageService.addTwoDept(map));
    }

}
